﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace _51_AOP.Utility.Filter
{
    public class CustomActionFilterAttribute : Attribute, IActionFilter
    {
        /// <summary>
        /// Action 执行之后
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnActionExecuted(ActionExecutedContext context)
        {
            Console.WriteLine("CustomActionFilterAttribute执行之后");
            var res = Newtonsoft.Json.JsonConvert.SerializeObject(context.Result);
            var controllerName = context.HttpContext.GetRouteValue("controller");
            var actionName = context.HttpContext.GetRouteValue("action");
            Console.WriteLine($"执行{controllerName}的{actionName}方法，执行结果={res}");
        }
        /// <summary>
        /// Action 执行之前
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnActionExecuting(ActionExecutingContext context)
        {
            Console.WriteLine("CustomActionFilterAttribute执行之前");
            var para = context.HttpContext.Request.QueryString.Value; //参数
            var controllerName = context.HttpContext.GetRouteValue("controller");
            var actionName = context.HttpContext.GetRouteValue("action");
            Console.WriteLine($"执行{controllerName}的{actionName}方法，参数={para}");

        }
    }
}
